package gframework.bean.passwordField;

import gframework.bean.Util;
import gframework.bean.field.GPlainDocument;
import java.awt.KeyboardFocusManager;
import java.util.HashSet;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class GPasswordField extends javax.swing.JPasswordField {
    
    public final static String MASK_NUMERO = "Numero";
    
    private String mascara = "";
    private int columns = 0;
    private boolean caixaAlta = true;
    
    public GPasswordField() {
        initComponents();
        
        setDocument(new GPlainDocument());

        HashSet kTab = new HashSet(1);
        kTab.add(KeyStroke.getKeyStroke("TAB"));
        kTab.add(KeyStroke.getKeyStroke("ENTER"));

        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, kTab);
    }
    
    @Override
    public int getColumns() {
        return columns;
    }
    
    public int getInt() {
        if (this.getText().equals("")) {
            return 0;
        } else {
            return Integer.parseInt(this.getText());
        }
    }
    
    @Override
    public void setColumns(int i_columns) {
        this.columns = i_columns;
        ((GPlainDocument) getDocument()).setColumns(i_columns);
    }

    public void setCaixaAlta(boolean i_caixaAlta) {
        caixaAlta = i_caixaAlta;
        ((GPlainDocument) getDocument()).setCaixaAlta(i_caixaAlta);
    }

    public boolean isCaixaAlta() {
        return caixaAlta;
    }
   
    public String getMascara() {
        return mascara;
    }
    
    public void setMascara(String i_mascara) {
        try {
            mascara = i_mascara;

            if (i_mascara.equals(MASK_NUMERO)) {
                ((GPlainDocument) getDocument()).setAcceptedChars(GPlainDocument.NUMERIC);
            }
            
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex.getMessage(), "VRPasswordField");
        }
    }
    
    public void setInt(int i_int){
        this.setText(String.valueOf(i_int));
    }

    @Override
    public void setText(String t) {
        super.setText(t);
        setBackground(new JTextField().getBackground());
    }

    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formActionPerformed(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                formFocusLost(evt);
            }
        });
        addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                formInputMethodTextChanged(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents
    
    private void formFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusLost

    }//GEN-LAST:event_formFocusLost
    
    
    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        if (isEditable()) {
            Runnable doSelect = new Runnable() {
                public void run() {
                    selectAll();
                }
            };

            SwingUtilities.invokeLater(doSelect);
        }
    }//GEN-LAST:event_formFocusGained

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
        setBackground(new JTextField().getBackground());
    }//GEN-LAST:event_formKeyTyped

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        
    }//GEN-LAST:event_formKeyPressed

    private void formActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_formActionPerformed
        
    }//GEN-LAST:event_formActionPerformed

    private void formInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_formInputMethodTextChanged
        
    }//GEN-LAST:event_formInputMethodTextChanged
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}